package com.aynu.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//使用WebMvcConfigurer可以扩展SpringMVC的功能
//@EnableWebMvc//全面接管SpringMVC
@Configuration//标注这是一个配置类
public class MyMvcConfig implements WebMvcConfigurer {
    //ctrl+o打开能重写的列表
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //拦截请求“xiaolang”并转发到"s"
        registry.addRedirectViewController("MyHello","s");
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

    }

    //所有WebMVCConfigureAdapter组件会一起起作用
    //前提是SpringBoot知道有这个组件
    //所以加上@Bean注解注册上
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("index");
//                registry.addViewController("/a").setViewName("dashboard");
                registry.addViewController("/b").setViewName("index");
            }
        };

//        //注册拦截器
//        @Override
//        public void addInterceptors(InterceptorRegistry registry) {
//            //super.addInterceptors(registry);
//            //静态资源；  *.css , *.js
//            //SpringBoot已经做好了静态资源映射
////                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
////                        .excludePathPatterns("/index.html","/","/user/login");
//        }
//    };

        return webMvcConfigurer;
    }

}
